﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace NovoSerDes
{
    public partial class NLS9246Pic : UIForm
    {
        private System.Drawing.Point mouseDownPoint;//存储鼠标焦点的全局变量
        private bool isSelected = false;//平移状态

        public NLS9246Pic()
        {
            InitializeComponent();
            this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);//添加滚轮事件
            this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
            this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
            this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
            this.pictureBox1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.panel_Picture_DoubleClick);
        }

        //滚轮缩放
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            
            if (e.Delta > 0)
            {
                pictureBox1.Width += (int)(pictureBox1.Width * 0.1);
                pictureBox1.Height += (int)(pictureBox1.Height * 0.1);
            }
            else if (e.Delta < 0)
            {
                pictureBox1.Width -= (int)(pictureBox1.Width * 0.1);
                pictureBox1.Height -= (int)(pictureBox1.Height * 0.1);
            }
        }

        //在MouseDown处获知鼠标是否按下，并记录下此时的鼠标坐标值；
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X;  //注：全局变量mouseDownPoint前面已定义为Point类型  
                mouseDownPoint.Y = Cursor.Position.Y;
                isSelected = true;
            }
        }

        //在MouseUp处获知鼠标是否松开，终止拖动操作；
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isSelected = false;
        }
        /// <summary>
        /// 判断鼠标是否在父窗口内移动
        /// </summary>
        /// <returns></returns>
        private bool IsMouseInPanel()
        {

            if (this.uiPanel1.Left < PointToClient(Cursor.Position).X
                    && PointToClient(Cursor.Position).X < this.uiPanel1.Left
                    + this.uiPanel1.Width && this.uiPanel1.Top
                    < PointToClient(Cursor.Position).Y && PointToClient(Cursor.Position).Y
                    < this.uiPanel1.Top + this.uiPanel1.Height)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //图片平移,在MouseMove处添加拖动函数操作
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isSelected && IsMouseInPanel())//确定已经激发MouseDown事件，和鼠标在picturebox的范围内
            {
                this.pictureBox1.Left = this.pictureBox1.Left + (Cursor.Position.X - mouseDownPoint.X);
                this.pictureBox1.Top = this.pictureBox1.Top + (Cursor.Position.Y - mouseDownPoint.Y);
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
            }
        }
        /// <summary>
        /// 双击还原
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_Picture_DoubleClick(object sender, EventArgs e)
        {
            this.pictureBox1.Left = 0;
            this.pictureBox1.Top = 0;
            this.pictureBox1.Width = uiPanel1.Width;
            this.pictureBox1.Height = uiPanel1.Height;
        }
    }
}
